#!/bin/sh

export MC="-j$(nproc)"

echo
echo "============================================"
echo "Install extensions from   : install.sh"
echo "PHP version               : ${PHP_VERSION}"
echo "Extra Extensions          : ${PHP_EXTENSIONS}"
echo "Extra Extensions Dir      : ${PHP_CONF_DIR}"
echo "Multicore Compilation     : ${MC}"
echo "Work directory            : ${PWD}"
echo "============================================"
echo


if [ "${PHP_EXTENSIONS}" != "" ]; then
    yum install -y gcc gcc-c++  make autoconf 
fi


export EXTENSIONS=",${PHP_EXTENSIONS},"


#
# Check if current php version is greater than or equal to
# specific version.
#
# For example, to check if current php is greater than or
# equal to PHP 7.0:
#
# isPhpVersionGreaterOrEqual 7 0
#
# Param 1: Specific PHP Major version
# Param 2: Specific PHP Minor version
# Return : 1 if greater than or equal to, 0 if less than
#
isPhpVersionGreaterOrEqual()
 {
    local PHP_MAJOR_VERSION=$(php -r "echo PHP_MAJOR_VERSION;")
    local PHP_MINOR_VERSION=$(php -r "echo PHP_MINOR_VERSION;")

    if [[ "$PHP_MAJOR_VERSION" -gt "$1" || "$PHP_MAJOR_VERSION" -eq "$1" && "$PHP_MINOR_VERSION" -ge "$2" ]]; then
        return 1;
    else
        return 0;
    fi
}


#
# Install extension from package file(.tgz),
# For example:
#
# installExtensionFromTgz redis-5.0.2
#
# Param 1: Package name with version
# Param 2: enable options
#
installExtensionFromTgz()
{
    tgzName=$1
    extensionName="${tgzName%%-*}"

    mkdir ${extensionName}
    tar -xf ${tgzName}.tgz -C ${extensionName} --strip-components=1
    ( cd ${extensionName} && phpize && ./configure && make ${MC} && make install )
}

#
# Add Conf File
# For example:
#
# installExtensionConf redis
#
# Param 1: Package name
#
installExtensionConf()
{
    extensionName=$1
    echo -e "; ${extensionName} extension ;\nextension=${extensionName}" > ${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini
    echo "----------- Add Conf File ---------------"
    echo "${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini";
}

#
# Add Zend Conf File
# For example:
#
# installExtensionConf redis
#
# Param 1: Package name
#
installZendExtensionConf()
{
    extensionName=$1
    echo -e "; ${extensionName} extension ;\nzend_extension=${extensionName}" > ${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini
    echo "----------- Add Conf File ---------------"
    echo "${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini";
}

#
# Install extension from php source file,
# For example:
#
# installExtensionFromExt pcntl
#
# Param 1: Package name
# Param 2: enable options
#
installExtensionFromExt()
{
    extensionName=$1
    extensionDIR="/usr/local/src/php-${PHP_VERSION}/ext/${extensionName}"

    ( cd ${extensionDIR} && phpize && ./configure ${options} && make ${MC} && make install )
}


if [[ -z "${EXTENSIONS##*,redis,*}" ]]; then
    echo "---------- Install redis ----------"
    isPhpVersionGreaterOrEqual 7 0
    if [[ "$?" = "1" ]]; then
        installExtensionFromTgz redis-5.0.2
    else
        printf "\n" | pecl install redis-4.3.0
    fi

    installExtensionConf redis
fi

if [[ -z "${EXTENSIONS##*,memcached,*}" ]]; then
    echo "---------- Install memcached ----------"
    yum install -y libmemcached libmemcached-devel
    isPhpVersionGreaterOrEqual 7 0

    if [[ "$?" = "1" ]]; then
        installExtensionFromTgz memcached-3.1.3
    else
        printf "\n" | pecl install memcached-2.2.0
    fi

    installExtensionConf memcached

fi

if [[ -z "${EXTENSIONS##*,xdebug,*}" ]]; then
    echo "---------- Install xdebug ----------"
    isPhpVersionGreaterOrEqual 7 0

    if [[ "$?" = "1" ]]; then
        isPhpVersionGreaterOrEqual 7 4
        if [[ "$?" = "1" ]]; then
            installExtensionFromTgz xdebug-2.9.2
        else
            installExtensionFromTgz xdebug-2.6.1
        fi
    else
        installExtensionFromTgz xdebug-2.5.5
    fi

    installZendExtensionConf xdebug
fi

if [[ -z "${EXTENSIONS##*,mongodb,*}" ]]; then
    echo "---------- Install mongodb ----------"
    installExtensionFromTgz mongodb-1.5.5
    installExtensionConf mongodb
fi


if [[ -z "${EXTENSIONS##*,opcache,*}" ]]; then
    echo "---------- Install opcache ----------"
    installExtensionFromExt opcache
    installZendExtensionConf opcache
fi

if [[ -z "${EXTENSIONS##*,swoole,*}" ]]; then
    echo "---------- Install swoole ----------"
    isPhpVersionGreaterOrEqual 7 0

    if [[ "$?" = "1" ]]; then
        installExtensionFromTgz swoole-4.4.2
    else
        installExtensionFromTgz swoole-2.0.11
    fi

    installExtensionConf swoole
fi

if [[ -z "${EXTENSIONS##*,xhprof,*}" ]]; then
    echo "---------- Install XHProf ----------"

    isPhpVersionGreaterOrEqual 7 0

    if [[ "$?" = "1" ]]; then
        mkdir xhprof
        tar -xf xhprof-2.2.0.tgz -C xhprof --strip-components=1
        ( cd xhprof/extension/ && phpize && ./configure  && make ${MC} && make install ) 

        installExtensionConf xhprof
    else
       echo "---------- PHP Version>= 7.0----------"
    fi

fi